package com.t.bricks.bussiness.service.useranddepartment.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.useranddepartment.UserAndDepartmentDao;
import com.t.bricks.bussiness.db.entity.useranddepartment.UserAndDepartment;
import com.t.bricks.bussiness.service.useranddepartment.UserAndDepartmentService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 人员部门'UserAndDepartment表'基本业务操作类接口实现
 */
@Service
public class UserAndDepartmentServiceImpl implements UserAndDepartmentService {

	@Autowired
	private UserAndDepartmentDao UserAndDepartmentDao;

	/**
	 * 根据关键值查数量
	 * @Param UserAndDepartment 存放查询条件的UserAndDepartment对象,注意:跨包访问时可能会是json字符串
	 * @return
	 */
	@Override
	public MsgEmity findCountByKey(Object UserAndDepartment) {
		if (null == UserAndDepartment) {
			return new MsgEmity(false, "条件对象不能为空！", 8000);
		}
		
		MsgEmity me = ModuleUtil.objToEntity(UserAndDepartment, UserAndDepartment.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		UserAndDepartment UserAndDepartmentData = me.getData();
		
		return UserAndDepartmentDao.findCountByKey(UserAndDepartmentData);
	}

	/**
	 * 批量保存人员所属部门关联信息
	 * 注意:若部门信息为空则会清除所有该人员与部门的关联信息
	 * @param sUserId 人员编号
	 * @param sDepamentList 部门编号集合,以','分隔
	 * @param sMainDepamentId 所属部门编号
	 * @return
	 */
	@Override
	public MsgEmity save(String sUserId, String sDepamentList, String sMainDepamentId) {
		if (null == sUserId || "".equals(sUserId.trim())) {
			return new MsgEmity(false, "人员编号参数不能为空！", 8001);
		}
		
		if (null == sDepamentList || "".equals(sDepamentList.trim())) {
			return UserAndDepartmentDao.del(sUserId.trim());
		}
		
		if (null == sMainDepamentId ||  "".equals(sMainDepamentId.trim())) {
			sMainDepamentId = "";
		} else {
			sMainDepamentId = sMainDepamentId.trim();
		}

		sUserId = sUserId.trim();
		String currentLoginUserId = ModuleUtil.currentLoginUserId();
		Date vNow = new Date();

		List<UserAndDepartment> deparmentList = new ArrayList<UserAndDepartment>();
		List<String> list = Arrays.asList(sDepamentList.split(","));// 部门编号集合转换
		for(int i = 0; i < list.size(); i++) {
			String sDeparmentId = list.get(i).trim();
			if ("".equals(sDeparmentId)) {
				continue;
			}
			
			UserAndDepartment pd = new UserAndDepartment();
			pd.setsCreator(currentLoginUserId);
			pd.setsDeparmentId(sDeparmentId);
			pd.setdCreateDate(vNow);
			pd.setdModifiedDate(pd.getdCreateDate());
			pd.setsModifieder(currentLoginUserId);
			pd.setsUserId(sUserId);
			pd.setiIndex(1);
			pd.setiState(sDeparmentId.equals(sMainDepamentId) ? 2 : 1);//所属部门的状态值为2,否则为1
			pd.setiVersion(1);
			deparmentList.add(pd);
		}
	 
		return UserAndDepartmentDao.save(sUserId, deparmentList);
	}
	
	/**
	 * 根据人员编号取人员与部门关联信息
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findBysUserId(String sUserId) {
		if (null == sUserId || "".equals(sUserId.trim())) {
			return new MsgEmity(false, "人员编号参数不能为空！", 8001);
		}
		
		return UserAndDepartmentDao.findBysUserId(sUserId.trim());
	}

	/**
	 * 根据部门编号查询员工
	 * @param sDeparmentId
	 * @param iMainDeparment 仅限主部门
	 * @return
	 */
	@Override
	public MsgEmity findByDepartmentId(String sDeparmentId, Integer iMainDeparment) {
		return UserAndDepartmentDao.findByDepartmentId(sDeparmentId, iMainDeparment);
	}
	
}
